package com.seanchenxi.gwt.wordpress.xmlrpc.service.impl;

import java.util.ArrayList;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.seanchenxi.gwt.wordpress.domain.model.Category;
import com.seanchenxi.gwt.wordpress.domain.model.FileData;
import com.seanchenxi.gwt.wordpress.domain.model.Post;
import com.seanchenxi.gwt.wordpress.domain.model.PostEditor;
import com.seanchenxi.gwt.wordpress.domain.model.WpFile;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.MetaWeblogServiceAsync;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.constant.HasMetaWeblogServiceMethodNames;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.CategoriesResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.PostResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.PostsResponseHandler;
import com.seanchenxi.gwt.xmlrpc.client.XmlRpcClient;
import com.seanchenxi.gwt.xmlrpc.client.XmlRpcRequest;

/**
 * The Class MetaWeblogServiceAsyncImpl.
 */
public class MetaWeblogServiceAsyncImpl extends XmlRpcClient implements
		MetaWeblogServiceAsync, HasMetaWeblogServiceMethodNames {

	/**
	 * Instantiates a new meta weblog service async impl.
	 *
	 * @param url the url
	 */
	public MetaWeblogServiceAsyncImpl(String url) {
		super(url);
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MetaWeblogServiceAsync#newPost(int, java.lang.String, java.lang.String, com.seanchenxi.gwt.wordpress.service.model.PostCreator, boolean, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void newPost(int blogId, String username, String password,
			PostEditor postCreator, boolean publish,
			AsyncCallback<Integer> callback) {
		XmlRpcRequest<Integer> xmlRpcRequest = createRequest(NEW_POST, blogId,
				username, password, postCreator, publish);
		send(xmlRpcRequest, callback);
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MetaWeblogServiceAsync#editPost(int, java.lang.String, java.lang.String, com.seanchenxi.gwt.wordpress.service.model.PostEditor, boolean, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void editPost(int postId, String username, String password,
			PostEditor postEditor, boolean publish,
			AsyncCallback<Boolean> callback) {
		XmlRpcRequest<Boolean> xmlRpcRequest = createRequest(EDIT_POST, postId,
				username, password, postEditor, publish);
		send(xmlRpcRequest, callback);
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MetaWeblogServiceAsync#getPost(int, java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getPost(int postId, String username, String password,
			AsyncCallback<Post> callback) {
		XmlRpcRequest<Post> xmlRpcRequest = createRequest(GET_POST, postId,
				username, password);
		send(xmlRpcRequest, callback, new PostResponseHandler());
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MetaWeblogServiceAsync#getRecentPosts(int, java.lang.String, java.lang.String, int, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getRecentPosts(int blogId, String username, String password,
			int number, AsyncCallback<ArrayList<Post>> callback) {
		XmlRpcRequest<ArrayList<Post>> xmlRpcRequest = createRequest(
				GET_RECENT_POST, blogId, username, password, number);
		send(xmlRpcRequest, callback, new PostsResponseHandler());
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MetaWeblogServiceAsync#getCategories(int, java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getCategories(int blogId, String username, String password,
			AsyncCallback<ArrayList<Category>> callback) {
		XmlRpcRequest<ArrayList<Category>> xmlRpcRequest = createRequest(
				GET_CATEGORIES, blogId, username, password);
		send(xmlRpcRequest, callback, new CategoriesResponseHandler<Category>());
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MetaWeblogServiceAsync#newMediaObject(int, java.lang.String, java.lang.String, com.seanchenxi.gwt.wordpress.service.model.FileData, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void newMediaObject(int blogId, String username, String password,
			FileData data, AsyncCallback<WpFile> callback) {
		// TODO will implement later
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MetaWeblogServiceAsync#getTemplate(int, java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getTemplate(int blogId, String username, String password,
			AsyncCallback<String> callback) {
		XmlRpcRequest<String> xmlRpcRequest = createRequest(GET_TEMPLATE,
				blogId, username, password);
		send(xmlRpcRequest, callback);
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MetaWeblogServiceAsync#setTemplate(int, java.lang.String, java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void setTemplate(int blogId, String username, String password,
			String content, AsyncCallback<Boolean> callback) {
		XmlRpcRequest<Boolean> xmlRpcRequest = createRequest(SET_TEMPLATE,
				blogId, username, password, content);
		send(xmlRpcRequest, callback);
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.MetaWeblogServiceAsync#deletePost(int, java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void deletePost(int postId, String username, String password,
			AsyncCallback<Boolean> callback) {
		XmlRpcRequest<Boolean> xmlRpcRequest = createRequest(DELETE_POST,
				postId, username, password);
		send(xmlRpcRequest, callback);
	}

}
